/* name: test
 * author: 5ab
 * created at: 2024-06-15
 */
#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define ssz(x) (int((x).size()))

auto chmax = [](auto& x, auto y) { if (x < y) x = y; };
auto chmin = [](auto& x, auto y) { if (y < x) x = y; };

using ll = long long;
const int N = 8000, M = 3e4;

int sx[N], sy[N], a[N];
bool isp[M + 1];

void solve()
{
	int n, m;
	
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> sx[i] >> sy[i];
	for (int i = 0; i < n; i++)
		cin >> a[i];
	isp[1] = 0;
	for (int i = 2; i <= m; i++)
		if (!isp[i])
		{
			for (int j = i * 2; j <= m; j += i)
				isp[j] = 1;
		}
	auto chk = [&](int d) {
		return !isp[d] && (d <= 10 || d % 20 == 3);
	};
	auto dis = [&](int i, int j) {
		return abs(sx[i] - sx[j]) + abs(sy[i] - sy[j]);
	};
	auto med = [&](int x, int y, int z) {
		return x + y + z - min({ x, y, z }) - max({ x, y, z });
	};
	
	ll sm = 0;
	for (int i = 0; i < n; i++)
		for (int j = i + 1; j < n; j++)
			for (int k = j + 1; k < n; k++)
			{
				int d = med(dis(i, j), dis(i, k), dis(j, k));
				// cerr << i << " " << j << " " << k << "  " << d << endl;
				if (chk(d))
					sm += ll(a[i]) + a[j] + a[k];
			}
	cout << sm << endl;
}

signed main()
{
	freopen("chess.in", "r", stdin);
	freopen("chess.out", "w", stdout);
	
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int cas;
	
	cin >> cas;
	while (cas--)
		solve();
	
	return 0;
}
// started coding at: 06-15 09:52:01